﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;
using LR4.Models;

namespace LR4.DAO
{
    public class MethodsDAO : DAO
    {
        public int GlobalId;

        public bool deleteOrder(int ID)               // Удаление заявки
        {
            bool result = true;
            Connect();
            try
            {
                SqlCommand cmd = new SqlCommand(
                    "DELETE FROM [Order] WHERE Contact_id=" + ID, Connection);

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                result = false;
            }
            finally
            {
                Disconnect();
            }
            return result;
        }


        public bool deleteProperty(int ID)               // Удаление недвижимости
        {
            bool result = true;
            Connect();
            try
            {
                SqlCommand cmd = new SqlCommand(
                    "DELETE FROM Property WHERE Id_f=" + ID, Connection);

                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                result = false;
            }
            finally
            {
                Disconnect();
            }
            return result;
        }


        public List<Mixed> GetAllRecords()        // Получение: получение контактных данных + заявки. 
        {                                         // Второй по значимости GET-метод в проекте
            Connect();  
            List<Mixed> MixedList = new List<Mixed>();

            try
            {
                SqlCommand command = new SqlCommand("SELECT ContactInfo.ID, ContactInfo.Name," +
                    " ContactInfo.Phone, [Order].Content_o FROM ContactInfo" + 
                    " INNER JOIN [Order] ON ContactInfo.ID = [Order].Contact_id", Connection);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Mixed mixed = new Mixed();
                    mixed.ID = Convert.ToInt32(reader["ID"]);
                    mixed.Name = Convert.ToString(reader["Name"]);
                    mixed.Phone = Convert.ToString(reader["Phone"]);
                    mixed.Content_o = Convert.ToString(reader["Content_o"]);
                    MixedList.Add(mixed);
                }
                reader.Close();
            }

            catch (Exception)
            {
            }

            finally
            {
                Disconnect();
            }
            return MixedList;
        }

      

        public bool addType(LR4.Models.Type type)        //Создание экземпляра класса "Type"
        {
            bool result = true;
            Connect();

            try
            {
                SqlCommand cmd = new SqlCommand(
                    "INSERT INTO [Type] (Property_type) " +
                    "VALUES (@Property_type)", Connection);

                cmd.Parameters.Add(new SqlParameter("@Property_type", type.Property_type));
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                result = false;
            }
            finally
            {
                Disconnect();
            }
            return result;
        }


        public bool addContactInfo(Mixed mixed)        //Добавление экземпляра класса "ContacInfo"
        {
            bool result = true;
            Connect();

            try
            {
                SqlCommand cmd = new SqlCommand(
                    "INSERT INTO ContactInfo (Name, Phone) " +
                    "VALUES (@Name, @Phone)", Connection);

                cmd.Parameters.Add(new SqlParameter("@Name", mixed.Name));
                cmd.Parameters.Add(new SqlParameter("@Phone", mixed.Phone));
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                result = false;
            }
            finally
            {
                Disconnect();
            }
            return result;
        }



        public bool addOrder(Mixed mixed)        //Добавление экземпляра класса "Order"
        {
            bool result = true;
            Connect();

            try
            {
                SqlCommand cmd = new SqlCommand(
                    "INSERT INTO [Order] (Content_o, Contact_id) " +
                    "VALUES (@Content_o, @Contact_id)", Connection);

                cmd.Parameters.Add(new SqlParameter("@Content_o", mixed.Content_o));
                cmd.Parameters.Add(new SqlParameter("@Contact_id", mixed.Contact_id));
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                result = false;
            }
            finally
            {
                Disconnect();
            }
            return result;
        }


        //Получение айди последней записи
        public int getLastContactId()
        {
            Connect();
            int ID = 0;
            try
            {
                SqlCommand command = new SqlCommand("SELECT * FROM ContactInfo WHERE " +
                    "(ID=(SELECT MAX(ID) FROM ContactInfo))", Connection);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {

                    ID = Convert.ToInt32(reader["ID"]);

                }
                reader.Close();
            }
            catch (Exception e)
            {
                //Error
                Console.Write("Ошибка:" + e.Message);
            }
            finally
            {
                Disconnect();
            }
            return ID;
        }



    }
}
